#ifndef _ARRAYLITERAL_HPP
#define _ARRAYLITERAL_HPP

#include <string>
#include "Token.hpp"
#include "Expression.hpp"
#include <map>
#include <vector>
#include <algorithm>

namespace ast {
	class ArrayLiteral : public Expression {
	public:
		ArrayLiteral(TOKEN token) {
			_token = token;
		}

		virtual void ExpressionNode() override{}


		virtual string tokenLiteral() override {
			return _token._literal;
		}
		
		virtual string String() override {
			string out;
			string tab = "    ";
			out.append("(\n");
			_depth++;
			size_t element_size = _elements.size();
			if (element_size) {
				std::vector<std::pair<Expression*, Expression*>> args(_elements.begin(), _elements.end());
				std::sort(args.begin(), args.end(), [](auto& a, auto& b) {
					return a.first->String() < b.first->String();
				});
				for (auto itr : args) {
					for (int i = 0; i < _depth; i++) {
						out.append(tab);
					}
					out.append("[");
					out.append(itr.first->String());
					out.append("] => ");
					out.append(itr.second->String());
					out.append("\n");
				}
			}
			for (int i = 0; i < _depth - 1; i++) {
				out.append(tab);
			}
			out.append(")");
			_depth--;
			return out;
		}

		virtual ~ArrayLiteral() {
			if (_elements.size()) {
				for (auto itr : _elements) {
					if (itr.first != nullptr) {
						delete itr.first;
					}
					if (itr.second != nullptr) {
						delete itr.second;
					}
				}
				_elements.clear();
			}
		}

	public:
		TOKEN _token;
		std::map<Expression*, Expression*> _elements;
	};
}

#endif